﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.JinShengService;
using System.Collections.ObjectModel;
using System.Windows.Data;

namespace CommunitySystem.PageView
{
    public partial class ZKZListControl : UserControl
    {
        private JinShengServiceClient _client = new JinShengServiceClient();
        public ZKZListControl()
        {
            InitializeComponent();

            Load();

        }
        private int pageSize = 20;
        private void Load()
        {
            selModelList.Clear();
            //分页  每页20条记录
            _client.GetZKZEmpInfoCountCompleted += new EventHandler<GetZKZEmpInfoCountCompletedEventArgs>(_client_GetZKZEmpInfoCountCompleted);
            _client.GetZKZEmpInfoCountAsync("");
            _client.GetZKZEmpInfoByPageCompleted += new EventHandler<GetZKZEmpInfoByPageCompletedEventArgs>(_client_GetZKZEmpInfoByPageCompleted);

            //_client.GetZKZEmpInfoByPageAsync("", 1, pageSize);

            //_client.GetZKZEmpInfoCompleted += (s, e) =>
            //{

            //    this.dgZKZList.ItemsSource = e.Result;

            //};
            //_client.GetZKZEmpInfoAsync("");



            _client.FBEmpZKZCompleted -= new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(_client_FBEmpZKZCompleted);
            _client.FBEmpZKZCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(_client_FBEmpZKZCompleted);

        }

        void _client_GetZKZEmpInfoCountCompleted(object sender, GetZKZEmpInfoCountCompletedEventArgs e)
        {
            ObservableCollection<int> list = new ObservableCollection<int>();
            int last = e.Result % pageSize;
            int count = 0;
            if (last > 0)
                count = e.Result / pageSize + 1;
            else
                count = e.Result / pageSize;
            for (int i = 0; i < count; i++)
            {
                list.Add(i);
            }

            PagedCollectionView pcv = new PagedCollectionView(list);
            dataPager1.PageSize = 1;
            dataPager1.Source = null;
            dataPager1.Source = pcv;
            //dataPager1_PageIndexChanged(null, null);
        }
        private ObservableCollection<ZKZEmpInfo> zkzModelList =
           new ObservableCollection<ZKZEmpInfo>();
        void _client_GetZKZEmpInfoByPageCompleted(object sender, GetZKZEmpInfoByPageCompletedEventArgs e)
        {
            
            this.dgZKZList.ItemsSource =zkzModelList= e.Result;
        }

        void _client_FBEmpZKZCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            string st = e.UserState as string;
            if (e.Error == null)
                SilverlightMessageBoxLibrary.Message.InfoMessage(st+"成功!");
            Load();
        }
        /// <summary>
        /// 复选框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private ObservableCollection<ZKZEmpInfo> selModelList =
           new ObservableCollection<ZKZEmpInfo>();
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            selModelList.Clear();

            CheckBox cb = sender as CheckBox;

            foreach (ZKZEmpInfo p in this.dgZKZList.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;

                if (p.IsSelected)
                {
                    if (selModelList.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selModelList.Add(p);
                    }
                }
                else
                {
                    if (selModelList.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selModelList.Remove(selModelList.Where(i => i.ID == selModel.ID).FirstOrDefault());
                    }
                }
            }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private bool zzz = false;
        private void dataPager1_PageIndexChanged(object sender, EventArgs e)
        {
            //int index = dataPager1.PageIndex;
            int index = dataPager1.PageIndex == -1 ? 0 : dataPager1.PageIndex;
           
            _client.GetZKZEmpInfoByPageAsync("", index + 1, pageSize);
        }
        /// <summary>
        /// 生成准考证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sczkz_Click(object sender, RoutedEventArgs e)
        {
            selModelList.Clear();
            foreach (var item in zkzModelList)
            {
                if (item.IsSelected == true)
                    selModelList.Add(item);
            }
            if (selModelList.Count == 0)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要生成准考证的人员！");
                return;
            }
            if (selModelList.Count > 0)
            {
                var sc = selModelList.Where(p => p.Status == "已生成").ToList();
                var fb = selModelList.Where(p => p.Status == "已发布").ToList();
                if (sc.Count > 0)
                {
                    string scname = "";
                    foreach (var item in sc)
                    {
                        scname += item.EmpName + ",";
                    }
                    SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage(string.Format("{0}准考证已生成过，需要重新生成吗！", scname.TrimEnd(',')), SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                    cm.Show();
                    cm.OKButton.Click += (sender1, e1) =>
                    {
                        AddZKZWindow zkz = new AddZKZWindow(selModelList);
                        zkz.Show();
                        zkz.Closed += new EventHandler(zkz_Closed);
                    };
                }
                else if (fb.Count > 0)
                {
                    string scname = "";
                    foreach (var item in sc)
                    {
                        scname += item.EmpName + ",";
                    }
                    SilverlightMessageBoxLibrary.Message.InfoMessage(string.Format("{0} 准考证已发布！", scname.TrimEnd(',')));
                    return;
                }
                else
                {
                    AddZKZWindow zkz = new AddZKZWindow(selModelList);
                    zkz.Show();
                    zkz.Closed += new EventHandler(zkz_Closed);
                }

            }


        }

        void zkz_Closed(object sender, EventArgs e)
        {
            Load();
        }
        /// <summary>
        /// 准考证预览
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ylzkz_Click(object sender, RoutedEventArgs e)
        {

            selModelList.Clear();
            foreach (var item in zkzModelList)
            {
                if (item.IsSelected == true)
                    selModelList.Add(item);
            }
            if (selModelList.Count == 0)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要预览准考证的人员！");
                return;
            }
            if (selModelList.Count != 1)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择一个人员进行预览！");
                return;
            }
            if (selModelList.Count == 1)
            {
                var sc = selModelList.Where(p => p.Status == "未生成").ToList();
                if (sc.Count > 0)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("请为此人员生成准考证后进行预览！");
                    return;
                }
            }
            var model = this.dgZKZList.SelectedItem as ZKZEmpInfo;

            ZKZYLWindow zkz = new ZKZYLWindow(selModelList.First());
            zkz.Show();
            zkz.Closed += new EventHandler(zkz_Closed);
        }
        /// <summary>
        /// 发布准考证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fbzkz_Click(object sender, RoutedEventArgs e)
        {
            selModelList.Clear();
            foreach (var item in zkzModelList)
            {
                if (item.IsSelected == true)
                    selModelList.Add(item);
            }
            if (selModelList.Count == 0)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要发布准考证的人员！");
                return;
            }
            else
            {
                var sc = selModelList.Where(p => p.Status == "未生成").ToList();
                string scname = "";
                foreach (var item in sc)
                {
                    scname += item.EmpName + ",";
                }
                if (sc.Count > 0)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage(scname.TrimEnd(',') + "准考证未生成，请生成准考证后进行发布！");
                    return;
                }
            }

            var ysc = selModelList.Where(p => p.Status == "已生成").ToList();
            string yscname = "";
            foreach (var item in ysc)
            {
                yscname += item.ID + ",";
            }

            _client.FBEmpZKZAsync(yscname.TrimEnd(','),"已发布","发布");
        }

        private ZKZEmpInfo selModel = null;
        private bool seSel = false;
        private void dgZKZList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            //if (this.dgZKZList.SelectedItem == null && seSel == false)
            //{
            //    seSel = true;
            //    return;
            //}
            //selModel = this.dgZKZList.SelectedItem as ZKZEmpInfo;
            //if (this.dgZKZList.SelectedItem != null && selModel != null && selModel.IsSelected == true && seSel == true)
            //{

            //    selModel.IsSelected = false;
            //    if (selModelList.Where(i => i.ID == selModel.ID).FirstOrDefault() != null)
            //        selModelList.Remove(selModelList.Where(i => i.ID == selModel.ID).FirstOrDefault());

            //    seSel = true;
            //    this.dgZKZList.SelectedItem = null;
            //    return;
            //}
            //else if (selModel != null)
            //{
            //    selModel.IsSelected = true;
            //    seSel = false;
            //    this.dgZKZList.SelectedItem = null;
            //}

            //if (selModel != null)
            //{
            //    if (selModel.IsSelected)
            //    {
            //        if (selModelList.Where(i => i.ID == selModel.ID).FirstOrDefault() == null)
            //        {
            //            selModelList.Add(selModel);
            //        }
            //    }
            //    else
            //    {
            //        if (selModelList.Where(i => i.ID == selModel.ID).FirstOrDefault() != null)
            //        {
            //            selModelList.Remove(selModelList.Where(i => i.ID == selModel.ID).FirstOrDefault());
            //        }
            //    }
            //}


        }

        private void dgZKZList_LoadingRow(object sender, DataGridRowEventArgs e)
        {

        }

        private void dgZKZList_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void cxzkz_Click(object sender, RoutedEventArgs e)
        {
            if (selModelList.Count == 0)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请选择要撤销发布准考证的人员！");
                return;
            }
         

            var ysc = selModelList.Where(p => p.Status == "已发布").ToList();
            string yscname = "";
            foreach (var item in ysc)
            {
                yscname += item.ID + ",";
            }
             SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确认要撤销吗?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
            cm.Show();
            cm.OKButton.Click += (sender1, e1) =>
            {
                _client.FBEmpZKZAsync(yscname.TrimEnd(','),"已生成","撤销发布");
            };
          
        }
    }
}
